package trial;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Picture;

import org.w3c.dom.Document;

public class Doc2Html {

	//Use Apache POI
	
	public void readDoc() throws IOException, ParserConfigurationException,
			TransformerFactoryConfigurationError, TransformerException {
		
		File file = new File("/home/noel/swp/xaoc/src/trial/samle-docs/haf_uebung1.doc");
		//File file = new File("/home/noel/swp/xaoc/src/trial/samle-docs/EBNF-Uebung11-12.docx");
		FileInputStream fis;
		fis = new FileInputStream(file.getAbsolutePath());
		System.out.println(file.getName());

		HWPFDocument document = new HWPFDocument(fis);
		
		
		//Picture
		List<Picture> pix = document.getPicturesTable().getAllPictures();
		
		//Picture Name
		String filename = pix.get(0).suggestFullFileName();
		System.out.println(filename);
		
		// where to save the picture
		String dir =  "/home/noel/swp/xaoc/src/trial/samle-docs/images/";
		System.out.println(dir);
		File outfile = new File(dir + filename);
		//File outfile = new File("c:/Users/nnn/Desktop/" + filename);
		FileOutputStream out = new FileOutputStream(outfile);
		out.write(pix.get(0).getContent());
		out.close();
		
		WordExtractor wordExtract = new WordExtractor(document);

		Document newDocument = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder().newDocument();
		WordToHtmlConverter wthc = new WordToHtmlConverter(newDocument);
		wthc.processDocument(document);

		StringWriter stringWriter = new StringWriter();
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
		transformer.setOutputProperty(OutputKeys.METHOD, "html");
		transformer.transform(new DOMSource(wthc.getDocument()),
				new StreamResult(stringWriter));

		String html = stringWriter.toString();

		System.out.println(html);
		
		// Save html into a file
		//File fil = new File("/home/noel/swp/xaoc/src/trial/samle-docs/new.html");
		//FileWriter fw = new FileWriter(fil);
		//fw.write(html);
		
		
		//InputStream is = new FileInputStream("d:/UNI/ss2014/swp/bspDOC/my.html");
		//XWPFDocument xdoc = new XWPFDocument(is);
		
		
		
	}

	public static void main(String[] args) throws IOException,
			ParserConfigurationException, TransformerFactoryConfigurationError,
			TransformerException {
		Doc2Html d = new Doc2Html();
		d.readDoc();

	}
}
